#region License
/* Copyright (c) 2011 Maksymilian Mulawa
	
Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
   3. The name of the author may not be used to endorse or promote products 
 * derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

using System;
using System.Text.RegularExpressions;

namespace ReplicationExplorer.DomainModel.Distributor.Entities
{
    public class SqlServerVersion
    {
        /// <summary>
        ///  Regular expression built for C# on: Thu, Jun 16, 2011, 09:52:55 AM
        ///  Using Expresso Version: 3.0.3634, http://www.ultrapico.com
        ///  
        ///  A description of the regular expression:
        ///  
        ///  [MajorVersion]: A named capture group. [\d{1,2}]
        ///      Any digit, between 1 and 2 repetitions
        ///  Literal .
        ///  [MinorVersion]: A named capture group. [\d{1,2}]
        ///      Any digit, between 1 and 2 repetitions
        ///  Literal .
        ///  [BuildNumber]: A named capture group. [\d{1,4}]
        ///      Any digit, between 1 and 4 repetitions
        /// </summary>
        public static Regex regex = new Regex(
              "(?<MajorVersion>\\d{1,2})\\.(?<MinorVersion>\\d{1,2})\\.(?<BuildNumber>\\d{1,4})",
            RegexOptions.CultureInvariant
            | RegexOptions.Compiled
            );

        public SqlServerVersion(string serverVersion)
        {
            Match serverVersionMatch = regex.Match(serverVersion);
            if(serverVersionMatch.Success)
            {
                MajorVersion = int.Parse(serverVersionMatch.Groups["MajorVersion"].Value);
                MinorVersion = int.Parse(serverVersionMatch.Groups["MinorVersion"].Value);
                BuildNumber = int.Parse(serverVersionMatch.Groups["BuildNumber"].Value);
            }
            else
            {
                throw new FormatException(
                    string.Format("Server version should match following format: major.minor.build, but was {0}", 
                    serverVersion));
            }
        }

        public int MajorVersion { get; set; }

        public int MinorVersion { get; set; }

        public int BuildNumber { get; set; }

        public bool  IsSqlServer2005OrHigher
        {
            get { return MajorVersion >= 9; }
        }
    }
}